﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebHttpApi.WebHttpApi.Core;

namespace WebHttpApi.WebHttpApi.HttpApi
{
    /// <summary>
    /// TyRTU 的摘要说明：RTU指令类
    /// </summary>
    public class TyRTU
    {
        public static string str_DeviceId = "";                //RTU地址逆序排列

        public static string str_MeterId = "";                 //表地址逆序排列
        public static string str_Comm = "";                    //命令串
        public static string str_CommPam = "";                 //参数串

        public static string str_zxjg = "0";                   //执行结果  -  0：未执行 | 1：已执行
        public static string str_xyjx = "";                    //协议解析  -  什么命令（日志）
        public static string str_rzlx = "存储";                //日志类型  -  存储：命令写数据库（日志）
        public static string str_xymc = "";                    //协议名称  -  命令类型名称： 控阀、休眠等（日志）
        public static string str_oper = "WebHttpApi";          //操作员    -  指令、日志

        public static string str_Result = "";

        public static string Rtu_Comm(string DeviceType, string Device_MeterId, string DeviceId, string Type, string CommPam)
        {
            str_xyjx = "";
            str_xymc = "";
            
            if (DeviceId != "" && Type != "")
            {
                try
                {
                    str_DeviceId = "";
                    for (int i = 0; i < DeviceId.Length / 2; i++)
                    {
                        string str_deviceid = DeviceId.Substring(2 * i, 2);
                        str_deviceid = Convert.ToString(Convert.ToInt64(str_deviceid), 16);
                        str_deviceid = "00" + str_deviceid;
                        str_deviceid = str_deviceid.Substring(str_deviceid.Length - 2);

                        str_DeviceId = str_deviceid + str_DeviceId;
                    }

                    switch (Type)
                    {
                        //查询GPS
                        //Type：5 | 05
                        case "5":
                            {
                                str_xyjx = "查询GPS";
                                str_xymc = "查询GPS";

                                str_Comm = str_DeviceId + "051000000100";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //写设备时间:指令下发写当前时间
                        //Type：6 | 06
                        case "6":
                            {
                                str_xyjx = "写设备时间";
                                str_xymc = "写设备时间";

                                try
                                {
                                    string str_year = DateTime.Now.ToString("yyyy");
                                    str_year = "0000" + str_year;
                                    str_year = str_year.Substring(str_year.Length - 4);

                                    string str_month = DateTime.Now.ToString("MM");
                                    str_month = "00" + str_month;
                                    str_month = str_month.Substring(str_month.Length - 2);

                                    string str_day = DateTime.Now.ToString("dd");
                                    str_day = "00" + str_day;
                                    str_day = str_day.Substring(str_day.Length - 2);

                                    string str_hour = DateTime.Now.ToString("HH");
                                    str_hour = "00" + str_hour;
                                    str_hour = str_hour.Substring(str_hour.Length - 2);

                                    string str_minute = DateTime.Now.ToString("mm");
                                    str_minute = "00" + str_minute;
                                    str_minute = str_minute.Substring(str_minute.Length - 2);

                                    string str_second = DateTime.Now.ToString("ss");
                                    str_second = "00" + str_second;
                                    str_second = str_second.Substring(str_second.Length - 2);

                                    str_CommPam = str_year + str_month + str_day + str_hour + str_minute + str_second;
                                }
                                catch
                                {
                                    str_Result = "写指令失败";
                                    break;
                                }

                                str_Comm = str_DeviceId + "0610000007" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);
                                break;
                            }

                        //控阀指令 
                        //Type：7 | 07
                        //CommPam：55、99、66 | 85、153、102
                        case "7":
                            {
                                if (CommPam == "85")
                                {
                                    str_CommPam = "55";
                                    str_xyjx = "开阀";
                                }
                                else if (CommPam == "153")
                                {
                                    str_CommPam = "99";
                                    str_xyjx = "关阀";
                                }
                                else if (CommPam == "102")
                                {
                                    str_CommPam = "66";
                                    str_xyjx = "光耦输出";
                                }
                                else
                                {
                                    str_Result = "参数错误";
                                    break;
                                }
                                str_xymc = "控阀";

                                if (DeviceType == "2")
                                {
                                    str_Comm = str_DeviceId + "0710000001" + str_CommPam;
                                    str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                    str_Comm = "1B06" + str_Comm;
                                    str_Comm = str_Comm.ToUpper();

                                    str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);
                                }
                                else
                                {
                                    str_Result = "不支持";
                                }
                                break;
                            }

                        //读程序版本
                        //Type：0C | 12
                        case "12":
                            {
                                str_xyjx = "读程序版本";
                                str_xymc = "读程序版本";

                                str_Comm = str_DeviceId + "0C10000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //读设备地址 | 清下行抄表地址
                        //Type：0D | 13
                        case "13":
                            {
                                str_xyjx = "读设备地址";
                                str_xymc = "读设备地址";

                                str_Comm = "AAAAAAAA" + "0D10000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //写设备地址
                        //Type：0E | 14
                        //CommPam：⑴、8位设备地址；
                        case "14":
                            {
                                str_xyjx = "写设备地址[" + CommPam + "]";
                                str_xymc = "写设备地址";

                                //新设备地址（逆序转16进制）
                                string str_new_DeviceId = "";         

                                try
                                {
                                    CommPam = "00000000" + CommPam;
                                    CommPam = CommPam.Substring(CommPam.Length - 8);

                                    for (int i = 0; i < CommPam.Length / 2; i++)
                                    {
                                        string str_deviceid = CommPam.Substring(2 * i, 2);
                                        str_deviceid = Convert.ToString(Convert.ToInt64(str_deviceid), 16);
                                        str_deviceid = "00" + str_deviceid;
                                        str_deviceid = str_deviceid.Substring(str_deviceid.Length - 2);

                                        str_new_DeviceId = str_deviceid + str_new_DeviceId;
                                    }

                                    str_CommPam = str_new_DeviceId;
                                }
                                catch
                                {
                                    str_Result = "写指令失败";
                                    break;
                                }

                                str_Comm = str_DeviceId + "0E10000004" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //参数设置
                        //Type：28 | 40
                        //CommPam：⑴、定时上传间隔(0-1440、HEX、低位在前)；
                        //CommPam：⑵、定时上传时间(2230 = 22:30、BCD、正序)；
                        //CommPam：⑶、定时存储间隔(0-1440、HEX、低位在前)；
                        //CommPam：⑷、定时存储开始时间(0130 = 01:30、BCD、正序)；
                        //CommPam：⑸、定时存储结束时间(0330 = 03:30、BCD、正序)；
                        //CommPam：⑹、24h存储间隔(0-1440、HEX、低位在前)；
                        case "40":
                            {
                                str_xyjx = "参数设置[" + CommPam + "]";
                                str_xymc = "参数设置";

                                try
                                {
                                    string[] str = CommPam.Split(':');

                                    //定时上传间隔
                                    string str_dsscjg = str[0];
                                    if (0 <= Convert.ToInt64(str_dsscjg) && Convert.ToInt64(str_dsscjg) <= 1440)
                                    {
                                        str_dsscjg = "0000" + Convert.ToInt64(str_dsscjg).ToString("x");
                                        str_dsscjg = str_dsscjg.Substring(str_dsscjg.Length - 4);

                                        str_dsscjg = str_dsscjg.Substring(2, 2) +
                                                     str_dsscjg.Substring(0, 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //定时上传时间
                                    string str_dsscsj = str[1];
                                    str_dsscsj = "0000" + str_dsscsj;
                                    str_dsscsj = str_dsscsj.Substring(str_dsscsj.Length - 4);

                                    if (0 <= Convert.ToInt64(str_dsscsj.Substring(0, 2)) && Convert.ToInt64(str_dsscsj.Substring(0, 2)) <= 23 && 0 <= Convert.ToInt64(str_dsscsj.Substring(2, 2)) && Convert.ToInt64(str_dsscsj.Substring(2, 2)) <= 59)
                                    {

                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //定时存储间隔
                                    string str_dsccjg = str[2];
                                    if (0 <= Convert.ToInt64(str_dsccjg) && Convert.ToInt64(str_dsccjg) <= 1440)
                                    {
                                        str_dsccjg = "0000" + Convert.ToInt64(str_dsccjg).ToString("x");
                                        str_dsccjg = str_dsccjg.Substring(str_dsccjg.Length - 4);

                                        str_dsccjg = str_dsccjg.Substring(2, 2) +
                                                     str_dsccjg.Substring(0, 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //定时存储开始时间
                                    string str_dscckssj = str[3];
                                    str_dscckssj = "0000" + str_dscckssj;
                                    str_dscckssj = str_dscckssj.Substring(str_dscckssj.Length - 4);

                                    if (0 <= Convert.ToInt64(str_dscckssj.Substring(0, 2)) && Convert.ToInt64(str_dscckssj.Substring(0, 2)) <= 23 && 0 <= Convert.ToInt64(str_dscckssj.Substring(2, 2)) && Convert.ToInt64(str_dscckssj.Substring(2, 2)) <= 59)
                                    {

                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //定时存储结束时间
                                    string str_dsccjssj = str[4];
                                    str_dsccjssj = "0000" + str_dsccjssj;
                                    str_dsccjssj = str_dsccjssj.Substring(str_dsccjssj.Length - 4);

                                    if (0 <= Convert.ToInt64(str_dsccjssj.Substring(0, 2)) && Convert.ToInt64(str_dsccjssj.Substring(0, 2)) <= 23 && 0 <= Convert.ToInt64(str_dsccjssj.Substring(2, 2)) && Convert.ToInt64(str_dsccjssj.Substring(2, 2)) <= 59)
                                    {

                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //24h存储间隔
                                    string str_24hccjg = str[5];
                                    if (0 <= Convert.ToInt64(str_24hccjg) && Convert.ToInt64(str_24hccjg) <= 1440)
                                    {
                                        str_24hccjg = "0000" + Convert.ToInt64(str_24hccjg).ToString("x");
                                        str_24hccjg = str_24hccjg.Substring(str_24hccjg.Length - 4);

                                        str_24hccjg = str_24hccjg.Substring(2, 2) +
                                                     str_24hccjg.Substring(0, 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    str_CommPam = str_dsscjg + str_dsscsj + str_dsccjg + str_dscckssj + str_dsccjssj + str_24hccjg;
                                }
                                catch
                                {
                                    str_Result = "写指令失败";
                                    break;
                                }

                                str_Comm = str_DeviceId + "281000000C" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);
                                break;
                            }

                        //参数读取
                        //Type：29 | 41
                        case "41":
                            {
                                str_xyjx = "参数读取";
                                str_xymc = "参数读取";

                                str_Comm = str_DeviceId + "2910000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //设置抄表波特率
                        //Type：2B | 43
                        //CommPam：⑴、波特率：00
                        //                     高字节：①0 = 2400、   ②1 = 4800、   ③2 = 9600
                        //                     低字节：①0 = 无校验、 ②1 = 奇校验、 ③2 = 偶校验 
                        //                     例：02 = 2400 偶校验
                        case "43":
                            {
                                str_xyjx = "设置抄表波特率[" + CommPam + "]";
                                str_xymc = "设置抄表波特率";

                                string str_bps = "00" + CommPam;
                                str_bps = str_bps.Substring(str_bps.Length - 2);

                                str_CommPam = str_bps;

                                str_Comm = str_DeviceId + "2B10000001" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //设置双IP发送
                        //Type：2C | 44
                        case "44":
                            {
                                str_xyjx = "设置双IP发送";
                                str_xymc = "设置双IP发送";

                                str_Comm = str_DeviceId + "2C1000000100";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //设置副IP端口
                        //Type：2D | 45
                        //CommPam：IP:端口
                        case "45":
                            {
                                str_xyjx = "设置副IP端口[" + CommPam + "]";
                                str_xymc = "设置副IP端口";

                                try
                                {
                                    string[] str = CommPam.Split(':');

                                    string str_ip = str[0];
                                    string[] ip = str_ip.Split('.');

                                    string str_ip01 = "00" + Convert.ToInt64(ip[0]).ToString("x");
                                    str_ip01 = str_ip01.Substring(str_ip01.Length - 2);
                                    string str_ip02 = "00" + Convert.ToInt64(ip[1]).ToString("x");
                                    str_ip02 = str_ip02.Substring(str_ip02.Length - 2);
                                    string str_ip03 = "00" + Convert.ToInt64(ip[2]).ToString("x");
                                    str_ip03 = str_ip03.Substring(str_ip03.Length - 2);
                                    string str_ip04 = "00" + Convert.ToInt64(ip[3]).ToString("x");
                                    str_ip04 = str_ip04.Substring(str_ip04.Length - 2);

                                    str_ip = str_ip01 + str_ip02 + str_ip03 + str_ip04;

                                    string str_dk = str[1];
                                    str_dk = Convert.ToInt64(str_dk).ToString("x");
                                    str_dk = "0000" + str_dk;
                                    str_dk = str_dk.Substring(str_dk.Length - 4);

                                    str_CommPam = str_ip + str_dk;
                                }
                                catch
                                {
                                    str_Result = "参数错误";
                                    break;
                                }

                                str_Comm = str_DeviceId + "2D10000006" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //读取副IP端口
                        //Type：2E | 46
                        case "46":
                            {
                                str_xyjx = "读取副IP端口";
                                str_xymc = "读取副IP端口";

                                str_Comm = str_DeviceId + "2E10000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //压力参数设置
                        //Type：30 | 48
                        //CommPam：⑴、压力存储间隔(0-1440、HEX、低位在前)；
                        //CommPam：⑵、压力上限(0001 = 0.1Mpa、BCD、低位在前)；
                        //CommPam：⑶、压力下限(5600 = 0.056Mpa、BCD、低位在前)；
                        case "48":
                            {
                                str_xyjx = "压力参数设置[" + CommPam + "]";
                                str_xymc = "压力参数设置";

                                try
                                {
                                    string[] str = CommPam.Split(':');

                                    //压力存储间隔
                                    string str_ylccjg = str[0];
                                    if (0 <= Convert.ToInt64(str_ylccjg) && Convert.ToInt64(str_ylccjg) <= 1440)
                                    {
                                        str_ylccjg = "0000" + Convert.ToInt64(str_ylccjg).ToString("x");
                                        str_ylccjg = str_ylccjg.Substring(str_ylccjg.Length - 4);

                                        str_ylccjg = str_ylccjg.Substring(2, 2) +
                                                     str_ylccjg.Substring(0, 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //压力上限
                                    string str_ylsx = str[1];
                                    str_ylsx = "0000" + Convert.ToInt64(Convert.ToDouble(str_ylsx) * 1000).ToString();
                                    str_ylsx = str_ylsx.Substring(str_ylsx.Length - 4);

                                    str_ylsx = str_ylsx.Substring(2, 2) +
                                               str_ylsx.Substring(0, 2);

                                    //压力下限
                                    string str_ylxx = str[2];
                                    str_ylxx = "0000" + Convert.ToInt64(Convert.ToDouble(str_ylxx) * 1000).ToString();
                                    str_ylxx = str_ylxx.Substring(str_ylxx.Length - 4);

                                    str_ylxx = str_ylxx.Substring(2, 2) +
                                               str_ylxx.Substring(0, 2);

                                    str_CommPam = str_ylccjg + str_ylsx + str_ylxx;
                                }
                                catch
                                {
                                    str_Result = "写指令失败";
                                    break;
                                }

                                str_Comm = str_DeviceId + "3010000006" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);
                                break;
                            }

                        //压力参数读取
                        //Type：31 | 49
                        case "49":
                            {
                                str_xyjx = "压力参数读取";
                                str_xymc = "压力参数读取";

                                str_Comm = str_DeviceId + "3110000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //设置设备参数
                        //Type：32 | 50
                        //CommPam：⑴、设备参数：00、1字节
                        //    +------------------------------------------------------------------------------------------+
                        //    |    BIT7   |   BIT6   |   BIT5   |   BIT4   |   BIT3   |   BIT2   |   BIT1  |    BIT0     |
                        //    --------------------------------------------------------------------------------------------
                        //    |    485    |   MBUS   |   脉冲   |   压力   |   阀门   |   输出   |   LoRa  | 实时采集状态| 
                        //    +------------------------------------------------------------------------------------------+
                        case "50":
                            {
                                str_xyjx = "设置设备参数[" + CommPam + "]";
                                str_xymc = "设置设备参数";

                                string str_sbcs = "00" + CommPam;
                                str_sbcs = str_sbcs.Substring(str_sbcs.Length - 2);

                                str_CommPam = str_sbcs;

                                str_Comm = str_DeviceId + "3210000001" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //瞬时流量监测设置
                        //Type：33 | 51
                        //CommPam：⑴、正向瞬时流量上限(2字节：0-6553.5m³/h、乘以10、HEX、低位在前)；
                        //CommPam：⑵、正向瞬时流量下限(2字节：0-6553.5m³/h、乘以10、HEX、低位在前)；
                        //CommPam：⑶、正向瞬时流量变化速率(1字节：HEX)；
                        //CommPam：⑷、报警量监测标志(1字节：①00 正向、②11 反向、③22 不监测、④33 正反向均监测)；
                        //CommPam：⑸、反向瞬时流量上限(2字节：0-6553.5m³/h、乘以10、HEX、低位在前)；
                        //CommPam：⑹、反向瞬时流量下限(2字节：0-6553.5m³/h、乘以10、HEX、低位在前)；
                        //CommPam：⑺、反向瞬时流量变化速率(1字节：HEX)；
                        case "51":
                            {
                                str_xyjx = "瞬时流量监测设置[" + CommPam + "]";
                                str_xymc = "瞬时流量监测设置";

                                try
                                {
                                    string[] str = CommPam.Split(':');

                                    //正向瞬时流量上限
                                    string str_zxsssx = str[0];
                                    if (0 <= Convert.ToDouble(str_zxsssx) && Convert.ToDouble(str_zxsssx) <= 6553.5)
                                    {
                                        str_zxsssx = "0000" + Convert.ToInt64(Convert.ToDouble(str_zxsssx) * 10).ToString("x");
                                        str_zxsssx = str_zxsssx.Substring(str_zxsssx.Length - 4);

                                        str_zxsssx = str_zxsssx.Substring(2, 2) +
                                                     str_zxsssx.Substring(0, 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //正向瞬时流量下限
                                    string str_zxssxx = str[1];
                                    if (0 <= Convert.ToDouble(str_zxssxx) && Convert.ToDouble(str_zxssxx) <= 6553.5)
                                    {
                                        str_zxssxx = "0000" + Convert.ToInt64(Convert.ToDouble(str_zxssxx) * 10).ToString("x");
                                        str_zxssxx = str_zxssxx.Substring(str_zxssxx.Length - 4);

                                        str_zxssxx = str_zxssxx.Substring(2, 2) +
                                                     str_zxssxx.Substring(0, 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //正向瞬时流量变化速率
                                    string str_zxssbhsl = str[2];
                                    str_zxssbhsl = "00" + Convert.ToInt64(str_zxssbhsl).ToString("x");
                                    str_zxssbhsl = str_zxssbhsl.Substring(str_zxssbhsl.Length - 2);

                                    //报警量监测标志
                                    string str_bjjcbz = str[3];
                                    str_bjjcbz = "00" + str_bjjcbz;
                                    str_bjjcbz = str_bjjcbz.Substring(str_bjjcbz.Length - 2);

                                    //反向瞬时流量上限
                                    string str_fxsssx = str[4];
                                    if (0 <= Convert.ToDouble(str_fxsssx) && Convert.ToDouble(str_fxsssx) <= 6553.5)
                                    {
                                        str_fxsssx = "0000" + Convert.ToInt64(Convert.ToDouble(str_fxsssx) * 10).ToString("x");
                                        str_fxsssx = str_fxsssx.Substring(str_fxsssx.Length - 4);

                                        str_fxsssx = str_fxsssx.Substring(2, 2) +
                                                     str_fxsssx.Substring(0, 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //反向瞬时流量下限
                                    string str_fxssxx = str[5];
                                    if (0 <= Convert.ToDouble(str_fxssxx) && Convert.ToDouble(str_fxssxx) <= 6553.5)
                                    {
                                        str_fxssxx = "0000" + Convert.ToInt64(Convert.ToDouble(str_fxssxx) * 10).ToString("x");
                                        str_fxssxx = str_fxssxx.Substring(str_fxssxx.Length - 4);

                                        str_fxssxx = str_fxssxx.Substring(2, 2) +
                                                     str_fxssxx.Substring(0, 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    //反向瞬时流量变化速率
                                    string str_fxssbhsl = str[6];
                                    str_fxssbhsl = "00" + Convert.ToInt64(str_fxssbhsl).ToString("x");
                                    str_fxssbhsl = str_fxssbhsl.Substring(str_fxssbhsl.Length - 2);

                                    str_CommPam = str_zxsssx + str_zxssxx + str_zxssbhsl + str_bjjcbz + str_fxsssx + str_fxssxx + str_fxssbhsl;
                                }
                                catch
                                {
                                    str_Result = "写指令失败";
                                    break;
                                }

                                str_Comm = str_DeviceId + "331000000B" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);
                                break;
                            }

                        //瞬时流量监测读取
                        //Type：34 | 52
                        case "52":
                            {
                                str_xyjx = "瞬时流量监测读取";
                                str_xymc = "瞬时流量监测读取";

                                str_Comm = str_DeviceId + "3410000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //设备休眠
                        //Type：37 | 55
                        case "55":
                            {
                                str_xyjx = "设备休眠";
                                str_xymc = "设备休眠";
                                
                                str_Comm = str_DeviceId + "371000000100";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //设备激活
                        //Type：38 | 56
                        case "56":
                            {
                                str_xyjx = "设备激活";
                                str_xymc = "设备激活";

                                str_Comm = str_DeviceId + "381000000100";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //设置脉冲表脉冲位
                        //Type：39 | 57
                        //CommPam：⑴、脉冲位：
                        //                       ①、2C：脉冲当量 0.001,两位小数；
                        //                       ②、2D：脉冲当量 0.01 ,一位小数；
                        //                       ③、2E：脉冲当量 0.1  ,零位小数；
                        case "57":
                            {
                                str_xyjx = "设置脉冲表脉冲位[" + CommPam + "]";
                                str_xymc = "设置脉冲表脉冲位";

                                string str_sbxmjh = "00" + CommPam;
                                str_sbxmjh = str_sbxmjh.Substring(str_sbxmjh.Length - 2);

                                str_CommPam = str_sbxmjh;

                                str_Comm = str_DeviceId + "3910000001" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //设置阀门最大执行时间
                        //Type：57 | 87
                        //CommPam：⑴、阀门最大执行时间（秒）：1字节HEX
                        case "87":
                            {
                                str_xyjx = "设置阀门最大执行时间[" + CommPam + "]";
                                str_xymc = "设置阀门最大执行时间";

                                string str_zdzxsj = "00" + Convert.ToInt64(CommPam).ToString("x");
                                str_zdzxsj = str_zdzxsj.Substring(str_zdzxsj.Length - 2);

                                str_CommPam = str_zdzxsj;

                                str_Comm = str_DeviceId + "5710000001" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }

                        //读取阀门最大执行时间
                        //Type：58 | 88
                        case "88":
                            {
                                str_xyjx = "读取阀门最大执行时间";
                                str_xymc = "读取阀门最大执行时间";

                                str_Comm = str_DeviceId + "581000000100";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = Rtu_sql(DeviceId, Device_MeterId, Type);

                                break;
                            }
                    }
                }
                catch
                {
                    str_Result = "参数错误";
                }
            }
            return str_Result;
        }

        public static string Rtu_sql(string DeviceId, string Device_MeterId, string Type)
        {
            DateTime str_dt = DateTime.Now;

            bool operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_cx(ybdz,sbdz,mllx,mlnr,bcsj,zxjg) values ('" +
                                                                Device_MeterId + "','" +
                                                                DeviceId + "','" +
                                                                Type + "','" +
                                                                str_Comm + "','" +
                                                                str_dt + "','" +
                                                                str_zxjg + "')");

            if (operatorlog == true)
            {
                operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_rz(ybdz,sbdz,xynr,xyjx,xtsj,rzlx,xymc,czy) values ('" +
                                                                    Device_MeterId + "','" +
                                                                    DeviceId + "','" +
                                                                    str_Comm + "','" +
                                                                    str_xyjx + "','" +
                                                                    str_dt + "','" +
                                                                    str_rzlx + "','" +
                                                                    str_xymc + "','" +
                                                                    str_oper + "')");
                if (operatorlog == true)
                {
                    str_Result = "成功";
                }
                else
                {
                    str_Result = "写日志失败";
                }
            }
            else
            {
                str_Result = "写指令失败";
            }

            return str_Result;
        }
    }
}